<?php

/**
 * StarManagement
 * The class to manage the students stars 
 */
class StarManagement
{
	private $classID = null;
	private $classDetails = null;
	
	/**
	 * __construct
	 * Sets the class information
	 * 
	 * @param int $classID The ID of the class to be managed
	 */
	function __construct($classID)
	{
		$this->classID = $classID;
		$this->classDetails = mysql_fetch_assoc(mysql_query("SELECT k.id, k.naam, s.naam AS schoolnaam FROM `klassen` AS k, `scholen` AS s WHERE k.id='" . $classID . "' AND k.school_id = s.id"));
		
		if(!$this->classDetails)
		{
			die('Could not retreive details of Class with ID: ' . $classID);
		}
	}
	
	/**
	 * showList
	 * returns a formatted table of the students of the given class ID
	 * 
	 * @return string with students table
	 */
	function showList()
	{
		$return = '<h1>Sterren beheren van leerlingen</h1><p>Klik op de gebruikersnaam van een leerling om zijn/haar sterren te wijzigen.</p>';
		$students = mysql_query("SELECT * FROM `leerlingen` WHERE `klas_id`='" . $this->classID . "' ORDER BY achternaam, voornaam ASC");

		$return .= '<table><tr><th>Gebruikersnaam</th><th>Voornaam</th><th>Achternaam</th></tr>';

		while($student = mysql_fetch_assoc($students))
		{
			$return .= '<tr>
				<td><a href="sterren/edit/' . $student['id'] . '">' . $student['gebruikersnaam'] . '</a></td>
				<td>' . $student['voornaam'] . '</td>
				<td>' . $student['achternaam'] . '</td>
			</tr>';
		}

		return $return . '</table>';
	}

	/**
	 * showEditForm
	 * handles the editing of stars from the given student
	 * 
	 * @param int $userID The ID of the student to be edited
	 * @return string Returns string errors if encountered
	 */
	function showEditForm($userID)
	{
		$student = mysql_fetch_assoc(mysql_query("SELECT * FROM `leerlingen` WHERE `id`='" . $userID . "'"));

		$formFields = array
		(
			'golden_stars' => array('Gouden sterren', 'gouden_sterren'),
			'silver_stars' => array('Zilveren sterren', 'zilveren_sterren'),
			'bronze_stars' => array('Bronzen sterren', 'bronzen_sterren')
		);

		// If the user does not exist
		if(gettype($student) != 'array')
		{
			return "Leerling niet gevonden.";
		}

		// Student not in teachers class
		if($student['klas_id'] != User::getClassId())
		{
			return "Leerling zit niet in deze klas!";
		}

		echo '<h1>Sterren van ' . $student['voornaam'] . ' ' . $student['achternaam'] . ' bewerken</h1><p>Op deze pagina kunt u het aantal sterren van de leerling bijstellen. Het bijstellen heeft een invloed op de <a href="top">top 10</a>. <strong>Let op:</strong> De aantallen kunnen niet naar boven bijgesteld worden!</p>';

		// If the form is submitted, process
		if($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			$errors = array();
			foreach($formFields as $key=>$values)
			{
				// submitted star amount is higher than the student previously had
				if($_POST[$key] > $student[$values[1]])
				{
					$errors[] = 'Waarde "' . $values[0] . '" is hoger dan de leerling heeft!';
				}

				// the submitted star value is not a digit
				if(!ctype_digit($_POST[$key]) || $_POST[$key] < 0)
				{
					$errors[] = 'Illegale waarde voor "' . $values[0] . '"';
				}
			}

			// if no errors encountered, update student, else print errors
			if(count($errors) == 0)
			{
				mysql_query("UPDATE `leerlingen` SET `gouden_sterren`='" . $_POST['golden_stars'] . "', `zilveren_sterren`='" . $_POST['silver_stars'] . "', `bronzen_sterren`='" . $_POST['bronze_stars'] . "' WHERE `id`=" . $student['id']);
				echo '<p><strong>De gebruiker is geupdate!</strong></p>';

				$student = mysql_fetch_assoc(mysql_query("SELECT * FROM `leerlingen` WHERE `id`='" . $student['id'] . "'"));
			}
			else
			{
				echo '<ul><li>' . implode('</li><li>', $errors) . '</li></ul>';
			}
		}

		echo '<form method="post"><table>';

		// loop the formfields
		foreach($formFields as $key=>$values)
		{
			echo '<tr>
				<td>' . $values[0] . ':</td>
				<td><select name="' . $key . '">';

			for($i = $student[$values[1]]; $i >= 0; $i--)
			{
				echo '<option>' . $i . '</option>';
			}

			echo '</select></td></tr>';
		}

		echo '<tr>
				<td></td>
				<td><input type="submit" value="Opslaan" /></td>
			</tr>
		</table></form>';
	}
}